import 'package:flutter/material.dart';
import 'util/SharedPreferencesUtil.dart';
import 'interfaces/SPHelp.dart';

class TestSP extends StatefulWidget {
  @override
  _TestSP createState() {
    return _TestSP();
  }
}

class _TestSP extends State<TestSP> {
  TextEditingController controller = TextEditingController();
  SharedPreferencesUtil sp = SharedPreferencesUtil.instance;
  String key = "sp";
  SPHelp spHelp = SPHelp();

  String value;

  _spSetString() {
//    spHelp.setUserName(controller.text);
    sp.setString(key, controller.text);
  }

  _spGetString() {
//    value = spHelp.getUserName();
//    setState(() {
//
//    });
    sp.getString(key).then((str) {
      value = str;
      setState(() {});
    });
  }

  @override
  void initState() {
    // TODO: implement initState
//    value = spHelp.getUserName();
    sp.getString(key).then((str) {
      value = str;
      setState(() {});
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("SharedPreferences"),
      ),
      body: Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              "$value",
              style: TextStyle(color: Colors.redAccent, fontSize: 16),
            ),
            TextField(
              controller: controller,
              decoration: InputDecoration(
                  contentPadding: EdgeInsets.all(10.0),
                  icon: Icon(Icons.add_to_home_screen),
                  labelText: "请输入要存储的内容",
                  helperText: "请输入。。。"),
              autofocus: false,
            ),
            RaisedButton(
              onPressed: _spSetString,
              child: Text("存储"),
            ),
            RaisedButton(
              child: Text("获取"),
              onPressed: _spGetString,
            )
          ],
        ),
      ),
    );
  }
}
